package com.cn.ccp.action.resourse;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ResourseBean;
import com.cn.ccp.util.Contants;

public class ResourseAction extends BaseAction{
	private Logger log = Logger.getLogger(ResourseAction.class);
	private List<ResourseBean> resourseList;//数据字典列表
	private ResourseBean resourseBean;
	private SelectItem[] typeSelectItem ;//资源类型下拉框
	private String resourseType = "-1";//资源类型
	public ResourseAction(){
		log.info("------------ResourseAction()---------------");
		getpagedetail();
		typeSelectItem = new SelectItem[4];
		typeSelectItem[0] = new SelectItem(String.valueOf(Contants.DATA_DICTIONARY_CLASS_TYPE) , getMessages("pages_resourse_type_course"));
		typeSelectItem[1] = new SelectItem(String.valueOf(Contants.DATA_DICTIONARY_LANGUAGE_TYPE) , getMessages("course_page_course_language_type"));
		typeSelectItem[2] = new SelectItem(String.valueOf(Contants.DATA_DICTIONARY_TEACHER_TYPE) , getMessages("user_page_label_teacher_type"));
		typeSelectItem[3] = new SelectItem(String.valueOf(Contants.DATA_DICTIONARY_TEACHER_TITLE_TYPE) , getMessages("user_page_label_positional"));
	}
	
	public void getpagedetail() {
		if(resourseType == null || "-1".equalsIgnoreCase(resourseType.trim()))
			setItemCount(getResourseService().getCountResourse());
		else
			setItemCount(getResourseService().getCountResourse(resourseType));
		int currentpage = getCurrentPage();
		int start = (currentpage - 1) * getPageSize();
		if(resourseType == null || "-1".equalsIgnoreCase(resourseType.trim()))
			resourseList = getResourseService().pageQueryResourse( start, getPageSize());
		else
			resourseList = getResourseService().pageQueryResourse( resourseType ,start, getPageSize());
	}
	
	public void selectTypeEvent(){
		getpagedetail();
	}
	
	
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null)
			resourseBean =  (ResourseBean)getResourseService().getResourseByID(id);
		log.info("-----------------"+resourseBean);
		return UPDATE;
	}
	
	public String doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		getResourseService().delResourse(id);
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
		return MAIN;
	}
	
	public String edit(){
		log.info("----------- edit()----------------");
		if(resourseBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		log.info("----------- save()----------------");
		boolean flag = getResourseService().resourseNameIsAlready(resourseBean.getType(), resourseBean.getName());
		if(flag){
			showMsg("name",getMessages("pages_resourse_name_is_used"));
			return "";
		}
		boolean flag2 =  getResourseService().saveResourse(resourseBean);
		if(flag2){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag =getResourseService().updateResourse(resourseBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}

	public List<ResourseBean> getResourseList() {
		return resourseList;
	}

	public void setResourseList(List<ResourseBean> resourseList) {
		this.resourseList = resourseList;
	}

	public ResourseBean getResourseBean() {
		if(resourseBean == null){
			resourseBean = new ResourseBean();
			resourseBean.setType(String.valueOf(Contants.DATA_DICTIONARY_CLASS_TYPE));
		}
		return resourseBean;
	}

	public void setResourseBean(ResourseBean resourseBean) {
		this.resourseBean = resourseBean;
	}

	public SelectItem[] getTypeSelectItem() {
		return typeSelectItem;
	}

	public String getResourseType() {
		return resourseType;
	}

	public void setResourseType(String resourseType) {
		this.resourseType = resourseType;
	}
	
}
